#include "stdafx.h"
#include "UtilFS.h"

CString Util::FS::CombinePath(CString str1, CString str2)
{
	for (int nIndex = str1.GetLength() - 1; nIndex >= 0; --nIndex)
	{
		if (str1.GetAt(nIndex) != _T('\\') && str1.GetAt(nIndex) != _T('/'))
		{
			str1 = str1.Left(nIndex + 1);
			break;
		}
	}

	for (int nIndex = 0; nIndex < str2.GetLength(); ++nIndex)
	{
		if (str2.GetAt(nIndex) != _T('\\') && str2.GetAt(nIndex) != _T('/'))
		{
			str2 = str2.Right(str2.GetLength() - nIndex);
			break;
		}
	}

	return str1 + _T("\\") + str2;
}

CString Util::FS::GetDirectoryOfFile(CString strFile)
{
	for (int nIndex = strFile.GetLength(); nIndex >= 0; --nIndex)
	{
		if (strFile.GetAt(nIndex) == _T('\\') || strFile.GetAt(nIndex) == _T('/'))
		{
			strFile = strFile.Left(nIndex);
			break;
		}
	}
	return strFile;
}

bool Util::FS::IsFileExist(CString strFilePath)
{
	WIN32_FIND_DATA stFindFileData;
	HANDLE hFind;

	hFind = FindFirstFile(strFilePath, &stFindFileData);
	return (hFind == INVALID_HANDLE_VALUE ? false : true);
}

CString Util::FS::GetTempPath()
{
	DWORD nLen = ::GetTempPath(0, NULL) + 1;
	TCHAR* lpBuffer = new TCHAR[nLen];
	::GetTempPath(nLen, lpBuffer);
	CString strPath = lpBuffer;
	return strPath;
}
